import 'package:flutter/material.dart';
import 'package:flutter_demo/demo/demo_page.dart';
import 'package:flutter_demo/pages/iframe_web_demo.dart';
import 'package:flutter_demo/pages/isolate_test/isolate_test_page.dart';
import 'package:flutter_demo/pages/squadron_worker_test/squadron_worker_test.dart';
import 'package:flutter_demo/pages/worker_manager/worker_manager_demo_1.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:worker_manager/worker_manager.dart';

Future<void> main() async {
  workerManager.log = true;
  await workerManager.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    EasyLoading.init();
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      // home: const MyHomePage(title: 'Flutter Demo Home Page'),
      home: Home(),
      builder: EasyLoading.init(),
    );
  }
}

class Controller extends GetxController {
  var count = 0.obs;
  //你可以使用一个简单的".obs "使任何变量成为可观察的
  incrment() => count++;
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //使用Get.put()实例化你的类，使其对当下的所有子路由可用。
    final Controller c = Get.put(Controller());

    return Scaffold(
      appBar: AppBar(
        title: Obx(() => Text("Click: ${c.count}")),
      ),
      //用一个简单的Get.to()即可代替Navigator.push那8行，无需上下文！
      body: Column(
        children: [
          ElevatedButton(
            child: Text("Go to Other"),
            onPressed: () => Get.to(() => DemoPage()), //官方推荐的，因为可以将controller的生命周期和widget绑定起来。
          ),
          SizedBox(height: 20,),
          ElevatedButton(
            child: Text("Isolate测试"),
            onPressed: () => Get.to(() => IsolateTestPage()),
          ),
          SizedBox(height: 20,),
          ElevatedButton(
            child: Text("Iframe示例"),
            onPressed: () => Get.to(() => IframeWebDemo()),
          ),
          SizedBox(height: 20,),
          ElevatedButton(
            child: Text("squadron-线程示例"),
            onPressed: () => Get.to(() => SquadronWorkerTest()),
          ),
          SizedBox(height: 20,),
          ElevatedButton(
            child: Text("worker-manager-线程示例"),
            onPressed: () => Get.to(() => WorkerManagerDemo1Page()),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: c.incrment,
      ),
    );
  }
}

class Other extends StatelessWidget {
  // 你可以让Get找到一个正在被其他页面使用的Controller，并将它返回给你。
  final Controller c = Get.find();

  @override
  Widget build(BuildContext context) {
    // 访问更新后的计数变量
    return Scaffold(
      body: Center(
        child: Text("${c.count}"),
      ),
    );
  }
}
